/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vigenere;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author triasfahrudin
 */
public class VigenereDec extends MIDlet implements CommandListener {

    private Form formMain;
    private Form formResult;
    private Display display;
    private TextField cipherText,key;
    private Command dec;
    
            
    public VigenereDec(){
        cipherText = new TextField("Cipher text", "", 30, TextField.ANY);
        key = new TextField("key", "",30, TextField.ANY);
        dec = new Command("DEC",Command.OK,2);
        
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        formMain = new Form("Vigenere Dec");
        formMain.append(cipherText);
        formMain.append(key);
        formMain.addCommand(dec);
        formMain.setCommandListener(this);
        display.setCurrent(formMain);        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    public void showResult(){
        display = Display.getDisplay(this);
        int tableRowSize = 26;
        int tableColumnSize = 26;
        int vigenereTable[][] = new int[26][26];
        
        //generate vigenere table
        for (int rows = 0;rows < tableRowSize;rows ++){
            for(int columns = 0;columns < tableColumnSize;columns++){
                vigenereTable[rows][columns] = (rows + columns) % 26;
            }
        }
        
        String sCipherText = cipherText.getString().toUpperCase();
        String sKey = key.getString().toUpperCase();
        
        String plainText = "";
        int keyIndex = 0;
        
        for (int ctextIndex = 0; ctextIndex < sCipherText.length(); ctextIndex++) {
            char cChar = sCipherText.charAt(ctextIndex);
            int asciiVal = (int) cChar;
            if (cChar == ' ') {
                plainText += cChar;
                continue;
            }

            if (asciiVal < 65 || asciiVal > 90) {
                plainText += cChar;
                continue;
            }

            int basiccipherTextValue = ((int) cChar) - 65;

            char kChar = sKey.charAt(keyIndex);
            int basicKeyValue = ((int) kChar) - 65;

            for (int pIndex = 0; pIndex < tableColumnSize; pIndex++) {
                if (vigenereTable[basicKeyValue][pIndex] == basiccipherTextValue) {
                    //char potcChar = (char) (vignereTable[basicKeyValue][pIndex] + 65);
                    char potpChar = (char) (pIndex + 65);
                    plainText += potpChar;
                }
            }

            keyIndex++;

            if (keyIndex == sKey.length()) {
                keyIndex = 0;
            }
        }
                
        formResult = new Form("Result");
        formResult.append("Plain Text: " + sCipherText);
        formResult.append("chiper Text: " + plainText);
        formResult.addCommand(new Command("Back",Command.BACK,1));
        formResult.setCommandListener(this);
        display.setCurrent(formResult); 
    }
    
    public void commandAction(Command c,Displayable d){
        String label = c.getLabel();
        if(label.equals("DEC")){
            showResult();
        } else if(label.equals("Back")){
            display.setCurrent(formMain);
        }
        
    }
}
